package com.ruoyi.dataMain.listener;

import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.dataMain.domain.DataAccount;
import com.ruoyi.dataMain.domain.bo.DataAccountBo;
import com.ruoyi.dataMain.domain.vo.DataAccountVo;
import com.ruoyi.dataMain.service.IDataAccountService;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 开户信息自定义导入
 *
 */
@Slf4j
public class DataAccountImportListener extends AnalysisEventListener<DataAccountVo> implements ExcelListener<DataAccountVo> {

    private final IDataAccountService iDataAccountService;
    private final String operName;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public DataAccountImportListener() {
        this.iDataAccountService = SpringUtils.getBean(IDataAccountService.class);
        this.operName = LoginHelper.getUsername();
    }

    @Override
    public void invoke(DataAccountVo dataAccountVo, AnalysisContext context) {
        DataAccountVo vo = this.iDataAccountService.queryByAccountId(dataAccountVo.getAccountId());
        DataAccountBo bo =  BeanUtil.toBean(dataAccountVo, DataAccountBo.class);
        try {
            // 验证是否存在这个帐户
            if (ObjectUtil.isNull(vo)) {
                ValidatorUtils.validate(bo);
                bo.setCreateBy(operName);
                iDataAccountService.insertByBo(bo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、账户 ").append(bo.getAccountId()).append(" 导入成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、账户 ").append(bo.getAccountId()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、账户 " + bo.getAccountId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<DataAccountVo> getExcelResult() {
        return new ExcelResult<DataAccountVo>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "部分数据导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<DataAccountVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
